<?php

namespace app\services\data\stat\Ad;

use app\services\sdk\GameProjectService;
use app\services\ServiceBase;

class StatBase extends ServiceBase
{
    protected $managerHost;
    protected $managerPwd;
    protected $sdkHost;
    protected $sdkPwd;

    protected $params;           // 查询参数
    protected $timezone;         // 时区
    protected $stime;            // 开始时间
    protected $etime;            // 结束时间
    protected $projectIds;       // 项目列表
    protected $gameIds;          // 游戏列表
    protected $channels;         // 渠道列表
    protected $gameChannelIds;   // 渠道应用列表
    protected $mediaIds;         // 媒体列表
    protected $mediaUserIds;     // 媒体账号
    protected $mediaDockingIds;  // 媒体对接类型
    protected $sourceType;       // 用户来源
    protected $deviceTypes;      // 设备类型列表
    protected $ownerIds;         // 投放人
    protected $dailiIds;         // 代理
    protected $planGroupIds;     // 计划组
    protected $planIds;          // 计划
    protected $groupbys;         // 分组
    protected $moneyTypeName;    // 金额类型 amount订单金额 money实际付款金额
    protected $designerId;       // 设计师ID
    protected $materialGroupId;  // 素材分组
    protected $fileType;         // 素材类型
    protected $screenType;       // 素材横竖屏
    protected $name;             // 素材名称
    protected $materialIds;      // 素材ID组


    public function __construct($params)
    {
        $this->managerHost = '172.16.0.201:3306';
        $this->managerPwd = 'rngX5YmR2snn9asT';
        $this->sdkHost = '172.16.0.201:3306';
        $this->sdkPwd = 'rngX6emR2snn9dux';

        $this->params         = $params;
        $this->timezone       = $params['timezone'] ?? 'Asia/Shanghai';
        $this->projectIds     = $params['project_id'] ?? [];
        $this->gameIds        = $params['game_id'] ?? [];
        $this->channels       = $params['channel'] ?? [];
        $this->gameChannelIds = $params['game_channel_id'] ?? [];
        $this->mediaIds       = $params['media_id'] ?? [];
        $this->mediaUserIds   = $params['media_user_id'] ?? [];
        $this->ownerIds       = $params['plan_owner_id'] ?? [];
        $this->dailiIds       = $params['daili_id'] ?? [];
        $this->planGroupIds   = $params['group_id'] ?? [];
        $this->planIds        = $params['plan_id'] ?? [];
        $this->sourceType     = $params['source_type'] ?? 0;
        $this->deviceTypes    = $params['device_type'] ?? [];
        $this->moneyTypeName   = $params['money_type_name'] ?? 'money';
        $this->designerId      = $params['designer_id'] ?? 0;
        $this->materialGroupId = $params['material_group_id'] ?? 0;
        $this->fileType        = $params['file_type'] ?? '';
        $this->screenType      = $params['screen_type'] ?? '';
        $this->name            = $params['name'] ?? '';
        $this->materialIds     = $params['material_ids'] ?? '';

        // 统计时间范围
        $today = strtotime('today');
        $tomorrow = strtotime('tomorrow');
        if (!empty($params['stat_date'])) {
            $this->stime = $params['stat_date'][0];
            $this->etime = $params['stat_date'][1];
        } else {
            $this->etime = strtotime('today')+86400;
            $this->stime = $this->etime - 86400*30;
        }
        if ($this->stime > $today) {
            $this->stime = $today;
        }

        // 如果选择了项目
        if (!empty($this->projectIds)) {
            $game_ids = GameProjectService::getInstance()->getGameIdByProjectPermission($this->projectIds);
            if (!empty($this->gameIds)) {
                $has_other_game = false;
                foreach ($this->gameIds as $val) {
                    if (!in_array($val, $game_ids)) {
                        $has_other_game = true;
                    }
                }
                if ($has_other_game) {
                    $this->gameIds = $game_ids;
                }
            } else {
                $this->gameIds = $game_ids;
            }
        }

        // 分组
        $groupbys = $params['groupbys'] ?? [];
        if (empty($groupbys)) {
            $groupbys = ['stat_date'];
        }
        // 如果设置了月分组，则不需要天和周
        if (in_array('stat_month', $groupbys)) {
            foreach ($groupbys as $key => $val) {
                if (in_array($val, ['stat_hour', 'stat_date', 'stat_week'])) {
                    unset($groupbys[$key]);
                }
            }
        }
        // 如果设置了周分组，则不需要天
        if (in_array('stat_week', $groupbys)) {
            foreach ($groupbys as $key => $val) {
                if (in_array($val, ['stat_hour', 'stat_date'])) {
                    unset($groupbys[$key]);
                }
            }
        }
        // 如果设置了天分组，则不需要小时
        if (in_array('stat_day', $groupbys)) {
            foreach ($groupbys as $key => $val) {
                if ($val == 'stat_hour') {
                    unset($groupbys[$key]);
                }
            }
        }
        $this->groupbys = array_values($groupbys);
    }

    public function getGroupbys()
    {
        return $this->groupbys;
    }

    public function getEtime()
    {
        return $this->etime;
    }
}
